require "Cocos2d"
require "date_unit"
require "date_kc"
require "date_game"
--require "records"

local CurGameOrder =nil --当前游戏索引
local scheduler = cc.Director:getInstance():getScheduler()
local s = cc.Director:getInstance():getWinSize()
local netTag = false
local XSMainMapScene=class("XSMainMapScene")
local _table={BX000000001={id="BX000000001",file="XueShiXing_Map_Scene.json",ui="XueShiXing_Map_Ui.json",touch="particle/game_main_map1/XSTouchDown.plist",move="particle/game_main_map1/touchMove.plist",index=1},
    BX000000002={id="BX000000002",file="ShuoShiXing_Map_Scene.json",ui="ShuoShiXing_Map_Ui.json",touch="particle/game_main_map1/SSTouchDown.plist",move="particle/game_main_map1/touchMove.plist",index=2},
    BX000000003={id="BX000000003",file="BoShiXing_Map_Scene.json",ui="BoShiXing_Map_Ui.json",touch="particle/game_main_map1/BSTouchDown.plist",move="particle/game_main_map1/touchMove.plist",index=3}}

local function get_index()
    local _id=gameManger.getCurSatrt()
    return _table[_id].index
end
local function get_id()
	return gameManger.getCurSatrt()
end
local function get_file()
    local _id=gameManger.getCurSatrt()
    
    return _table[_id].file
end
local function get_touch()
    local _id=gameManger.getCurSatrt()
    return _table[_id].touch
end
local function get_move()
    local _id=gameManger.getCurSatrt()
    return _table[_id].move
end
local function get_ui()
    local _id=gameManger.getCurSatrt()
    return _table[_id].ui
end
XSMainMapScene.__index=XSMainMapScene
XSMainMapScene._uiLayer= nil
XSMainMapScene.m_scene_root = nil
XSMainMapScene._gameSelect=nil
XSMainMapScene._miniLayer=nil
XSMainMapScene._type_inedx=nil
XSMainMapScene._class_index=nil
XSMainMapScene._game_index=nil
XSMainMapScene._isCanRote=true
XSMainMapScene._dyIndex=1
XSMainMapScene.__hua=nil
XSMainMapScene._isMini=false
XSMainMapScene._rootPanel=nil
XSMainMapScene._scrollView=nil
XSMainMapScene._isOut=false
XSMainMapScene._Panel_LeftUi=nil
XSMainMapScene._Panel_RightUi=nil
XSMainMapScene._Panel_pet=nil-- 头像
XSMainMapScene._canTouch=nil--当点击一个按钮时其它按钮是否可以点击的控制变量--by mitsui
XSMainMapScene.m_nSoundId=nil
--XSMainMapScene.m_Ac=nil
XSMainMapScene.boxTag = false
XSMainMapScene.isIn = false -- 是否进入游戏
XSMainMapScene._game_id=""
local _cur_x=0 -- 当前scollview位置
function XSMainMapScene.extend(target)
    local t = tolua.getpeer(target)
    if not t then
        t = {}
        tolua.setpeer(target, t)
    end
    setmetatable(t, XSMainMapScene)
    return target
end
local _gameList=nil
local _gameScoreList=nil
local _listView=nil
function XSMainMapScene:show_mini_layer(classTypeIndex,unitIndex,classIndex,gameIndex,score)
    --    local _gameList=local_data.get_game_list_by_classType(gameManger.getCurSatrt())--当前星球游戏列表**
    self._canTouch = false--by mitsui
    self._miniLayer:setVisible(true)
    _listView=self._miniLayer:getChildByName("Panel_left"):getChildByName("ListView")
    -------------------------------------------------排行帮
    local function init_list(_table)
        if self._miniLayer ==nil then return  end

        --table_out("----",_table)
        for k,v in ipairs(_table) do
            local _node=ccs.GUIReader:getInstance():widgetFromJsonFile("endlayerSingleUi_1.json")
            local items = _listView:getItems()
            local items_count = table.getn(items)
            _listView:insertCustomItem(_node, items_count)
            _listView:doLayout()
            _listView:getInnerContainer():sortAllChildren()
            local _item=_node:getChildByName("Image_1")
            local _name=_item:getChildByName("Label_name")
            local _score=_item:getChildByName("Label_score")
            local _avt=_item:getChildByName("Image_avt")
            if v.pet_id~=nil then
                _avt:loadTexture(v.pet_id..".png")
            end
            _name:setString(v.name)
            _score:setString(v.score.."分")
        end
        
    end
    if score==nil then score=0 end
    local unitId = local_data.get_unit_id_by_unitIndex(gameManger.getCurSatrt(),unitIndex)
    local _game_id=local_data.get_gameId_by_cIdx_gIdx(gameManger.getCurSatrt(),unitId,classIndex,gameIndex)
    gameManger.get_game_rank(_game_id,function (_state,_mes)
        if _state==200 then
            local _table=json.decode(_mes).data
            --table_out("game_rank",_table)
            if _table==nil or #_table<1 then
                return
            end
            table.sort(_table,function(a,b)
                return a.rank<b.rank
            end)
            init_list(_table)
        end
    end)
    ----------------------------------------------------
    local _panel=self._miniLayer:getChildByName("Panel_title")
    local _Image_title=_panel:getChildByName("Image_title")
    local _buttonBegin = _panel:getChildByName("Button_begin")

    local numstr = string.format("%02d",(classIndex-1)*3+gameIndex)
    _Image_title:loadTexture("YX"..get_index().."-"..unitIndex.."-"..classIndex.."-"..gameIndex..".png")
    local _gameScoreList=gameManger.get_score_list()--成绩列表
    if #_gameScoreList < tonumber(numstr) then -- 如果这个游戏还未开
        _buttonBegin:setVisible(false)
    else
        _buttonBegin:setVisible(true)
    end
    ccs.ActionManagerEx:getInstance():playActionByName("mini_1_1_1_Ui.json","out")

    local _gnam=_panel:getChildByName("Panel_title"):getChildByName("Label_title_game")
--    require "records"
    local _unit_id = local_data.get_unit_id_by_unitIndex(gameManger.getCurSatrt(),unitIndex)
    local gameId = local_data.get_gameId_by_cIdx_gIdx(gameManger.getCurSatrt(),_unit_id,classIndex,gameIndex)
    local gameOrder = local_data.get_game_order_classType_gameId(gameManger.getCurSatrt(),gameId)
    _gnam:setString(gameManger.get_gname_bugameid(_gameList[gameOrder]["id"]))
    local _starPanel=_panel:getChildByName("Panel_start")
    local _starts={}
    table.insert(_starts,_starPanel:getChildByName("Image_1_0"))
    table.insert(_starts,_starPanel:getChildByName("Image_2_0"))
    table.insert(_starts,_starPanel:getChildByName("Image_3_0"))
    for k,v in ipairs(_starts) do
        v:setVisible(false)
    end
    
    self._game_id=_gameList[gameOrder]["id"]
    --cclog("----->ssss"..gameOrder)
    local starNum = 0
    if _gameScoreList[gameOrder]~= nil then
        starNum = gameManger.change_scoreToStart(_gameScoreList[gameOrder].score)
    end
    if starNum>0 then
        for var=1, starNum do
            _starts[var]:setVisible(true)
        end
    end
    if self.m_nSoundId~=nil then
        AudioEngine.stopEffect(self.m_nSoundId)

    end
    if classTypeIndex==1 and unitIndex==1 then
        self.m_nSoundId= AudioEngine.playEffect("read/reading"..unitIndex.."-"..classIndex.."-"..gameIndex..".mp3")
    else
        self.m_nSoundId= AudioEngine.playEffect("read/reading"..classTypeIndex.."-"..unitIndex.."-"..classIndex.."-"..gameIndex..".mp3")
    end
    if #_gameScoreList < tonumber(numstr) then -- 如果这个游戏还未开
        _buttonBegin:setVisible(true)
    end
end
function XSMainMapScene:init()
    self._uiLayer =cc.Layer:create()
    --    getBG()
    self:addChild(self._uiLayer)
    self.m_scene_root =
        ccs.SceneReader:getInstance():createNodeWithSceneFile(get_file())
    self._uiLayer:addChild(self.m_scene_root)

    self._canTouch = true--by mitsui
    self._rootPanel = self.m_scene_root:getChildByTag(10003):getChildByTag(1)
    self._Panel_LeftUi=self._rootPanel:getChildByName("Panel_LeftUi")
    self._Panel_RightUi=self._rootPanel:getChildByName("Panel_RightUi")
    self._scrollView = self._rootPanel:getChildByName("ScrollView_2")
    --    self._scrollView:setVisible(false)
    self._Panel_pet = self._scrollView:getChildByName("Panel_pet")
    local headImg = self._Panel_pet:getChildByName("Image_head")
    headImg:setLocalZOrder(2)
    local popImg = self._Panel_pet:getChildByName("Image_pop")
    popImg:setLocalZOrder(3)
    self._Panel_pet:getChildByName("Image_head"):loadTexture(client.get_pet_icon()) -- 获取头像
    self._Panel_pet:setVisible(false)
    --    self._scrollView:setPositionType(ccui.PositionType.percent)
    local homeButton = self._Panel_LeftUi:getChildByName("Button_home") -- 回到主页按钮
    homeButton:addTouchEventListener(function(sender,eventType)
        if eventType == ccui.TouchEventType.ended  then
            AudioEngine.playEffect("uimusic/anniu.mp3",false)
            StartMain_Page_index = 0
            CurGameOrder = nil
            isNewGame=false
            self:runAction(cc.Sequence:create(cc.DelayTime:create(0.2),cc.CallFunc:create(function() 
                goUI(_allUI.homeScene)
            end)))
            
            
        end
    end)
    local studentsButton = self._Panel_LeftUi:getChildByName("Button_ranking") -- 回到主页按钮
    studentsButton:addTouchEventListener(function(sender,eventType)
        if eventType == ccui.TouchEventType.ended  then
            AudioEngine.playEffect("uimusic/anniu.mp3",false)
            require "waitLoad"
            waitLoad.show_net_wait()
            gameManger.get_class_info(get_id(),function (_state,_mes)
                waitLoad.dell_net_wait()
                if _state==200 then
                    if netTag == false then return end
                    local _table=json.decode(_mes).data
                    require "students/studentsLayer"
                    show_students(get_id(),_table)
                    return
                end
                waitLoad.show_ui_mes("网络不给力稍后再试")
            end)
        end
    end)
    -- 单元一
    self.panel_Danyuan_1 = self._scrollView:getChildByName("Panel_danyuan_1")
    local x=self.panel_Danyuan_1:getPositionX()
    local y=self.panel_Danyuan_1:getPositionY()
    self._scrollView:jumpToPercentHorizontal(0)
    ---
    self:Adaption()
    self:initGameSelect()
    self:init_select()
    self:initScrollViewTouch()--特殊粒子效果
end

function XSMainMapScene:Adaption()-- 适配
    local s = cc.Director:getInstance():getVisibleSize()
    local bi = 768/s.height
    local m = 1382/bi
    local L_x = m-s.width
    --    local sz = self._scrollView:getContentSize()
    --    self._scrollView:setContentSize(sz.width-L_x/2,sz.height)
    --    self._scrollView:setPositionX(L_x/2)
    --    self.m_scene_root:getChildByTag(10004):setPositionX(L_x/2)--回退按钮
    self._Panel_LeftUi:setPositionX(L_x/2) -- 左侧ui
    self._Panel_RightUi:setPositionX(s.width+L_x/2-self._Panel_RightUi:getContentSize().width) --右侧ui
end

function XSMainMapScene:initGameSelect()-- 初始化选 “饼” 后的弹窗
    self._miniLayer= ccs.GUIReader:getInstance():widgetFromJsonFile("mini_1_1_1_Ui.json")
    self._uiLayer:addChild(self._miniLayer,100)
    self._miniLayer:setVisible(false)
    if self.m_nSoundId~=nil then
        AudioEngine.stopEffect(self.m_nSoundId)
    end

    local _nextButton=self._miniLayer:getChildByName("Panel_title"):getChildByName("Button_begin")

    _nextButton:addTouchEventListener(function(sender,eventType)
        if eventType == ccui.TouchEventType.ended and self.isIn==false then
            if self.m_nSoundId~=nil then
                AudioEngine.stopEffect(self.m_nSoundId)
            end
            AudioEngine.playEffect("uimusic/anniu.mp3",false)
            require "gameManger"
            gameManger.go_game(self._game_id)
            self.isIn = true
            _cur_x=self._scrollView:getInnerContainer():getPositionX()
            --            self.panel_Danyuan_1:getPositionX()
            --            --cclog("x----------------->".._cur_x)
        end
    end)
    local _back=self._miniLayer:getChildByName("Panel_title"):getChildByName("Button_close")
    _back:addTouchEventListener(function(sender,eventType)
        if eventType == ccui.TouchEventType.ended then
            if self.m_nSoundId~=nil then
                AudioEngine.stopEffect(self.m_nSoundId)
            end
            --        --cclog("jj;")
            AudioEngine.playEffect("uimusic/anniu.mp3",false)
            self._miniLayer:setVisible(false)
            _listView:removeAllItems()
            --        _listView
            self._isMini=false
            self._canTouch = true--by mitsui
        end
    end)
    ccs.ActionManagerEx:getInstance():playActionByName(get_ui(),"game_pet_Ac")-- 头像

end
function XSMainMapScene:init_ui_state()--初始化ui状态
    local _unitList=gameManger.get_unit_list()--单元列表
    local _courseList=gameManger.get_course_list()--课程列表
    local _gameScoreList=gameManger.get_game_score(nil)--游戏列表

end
function XSMainMapScene:init_select() -- 初始化主页面
    self:loadGame()
end

function XSMainMapScene:loadGame()
    --if 1==1 then return  end
--    local _root = self.panel_Danyuan_1
    local StarLabel =self._Panel_RightUi:getChildByName("Label_star_num")
    StarLabel:setString(""..client.Info.userInfo.star_count)
    local AppleLabel = self._Panel_RightUi:getChildByName("Label_apple")
    AppleLabel:setString(""..client.Info.userInfo.apple_count)
    --    self._table =
    --    if gameManger.is_music_over("KC000000111",function(_state,_mes) end) ==true then
    --
    --    end
    netWork.get_total_star(function (_state,_mes)--动态更新xx
        if _state==200 then
            if netTag == false then return end
            if AppleLabel~=nil and AppleLabel~=nil then
                AppleLabel:setString(""..client.Info.userInfo.apple_count)
                StarLabel:setString(""..client.Info.userInfo.star_count)
            end
    end
    end)
    
    -------------- 相关方法开始 --------------------
    local function open_er(unitIndex,classIndex)
        _root = self._scrollView:getChildByName("Panel_danyuan_"..unitIndex)
        local _ergeButton = _root:getChildByName("Panel_Class_"..classIndex):getChildByName("Panel_Song"):getChildByName("Button")
        _ergeButton:setOpacity(255)
        if _ergeButton:isTouchEnabled()==false then -- 儿歌可点击
            _ergeButton:setTouchEnabled(true)
        end
        require "gameManger"
        local has_opened = false --是否打开过
        require "local_data"
        local unit_id = local_data.get_unit_id_by_unitIndex(gameManger.getCurSatrt(),unitIndex)
        local _couse_id=local_data.get_courseID_by_claId_uniId_Index(gameManger.getCurSatrt(),unit_id,classIndex)
        local ergeAc=nil
        gameManger.is_music_over(_couse_id,function(_state,_mes)
            has_opened = true
            ergeAc=ccs.ActionManagerEx:getInstance():playActionByName(get_ui(),"erg_Pao_"..unitIndex.."_"..classIndex.."_Ac")
        end)  --儿歌泡泡
        local function award() -- 发奖
            --
            if ergeAc~= nil then
                ergeAc:stop()
                _ergeButton:getParent():getChildByName("Image_Pao"):setOpacity(0)
        end
        local p =cc.ParticleSystemQuad:create("particle/box/box_star_getScore.plist")
        local Bworldp = _ergeButton:convertToWorldSpaceAR(cc.p(0,0))
        local pos = self._rootPanel:convertToNodeSpace(Bworldp)
        p:setPosition(pos)
        self._rootPanel:addChild(p,333)
        --cclog("ttttt")
        p:runAction(cc.Sequence:create(cc.MoveTo:create(1.5,cc.p(self._Panel_RightUi:getPositionX()+90,self._Panel_RightUi:getPositionY()+55)),cc.CallFunc:create(function()
            AudioEngine.playEffect("uimusic/addstar.mp3",false)
            local StarLabel =self._Panel_RightUi:getChildByName("Label_star_num")
            local num = tonumber(StarLabel:getString())
            num = num + 1
            StarLabel:setString(""..num)
            p:removeFromParent(true)
        end)))
        end
        local function get_mv_path(_couse_id)
            local _local={"KC000000111","KC000000112","KC000000113","KC000000114","KC000000211","KC000000311"}
            for k,v in ipairs(_local) do
            	if v==_couse_id then
            	return  "mv/".._couse_id
            	end
            end
            return cc.FileUtils:getInstance():getWritablePath().."mv/".._couse_id
        end
        _ergeButton:addTouchEventListener(function(sender,eventType)
            if eventType == ccui.TouchEventType.ended then
                --弹出首次页面
                if has_opened == true then
                    require "waitLoad"
                    waitLoad.show_mv_mes(function()
                    cclog("--mv__course-->".._couse_id)
                        gameManger.play_mv(get_mv_path(_couse_id),true,function(_isComplete)
                            --cclog("_isComplete-------->")
                            if _isComplete==true then--视频播放
                                gameManger.play_music_over(_couse_id,function(_state,_mes)
                                    if _state==200 then
                                        if netTag == false then return end
                                        --cclog("_state-->".._state)
                                        --cclog("_mes-->".._mes)
                                        waitLoad.dell_net_wait()
                                        award()
                                        has_opened=false
                                    end
                                end)
                            end
                        end)
                    end)
                else
                    --cclog("第二次了")
                    gameManger.play_mv(get_mv_path(_couse_id),false)
                end
            end
        end)

    end

    
    local function Open_Door( unitIndex,classIndex ) -- 普通初始化门的开闭
        _root = self._scrollView:getChildByName("Panel_danyuan_"..unitIndex)
        local doorclose = _root:getChildByName("Panel_Class_"..classIndex):getChildByName("Panel_Door"):getChildByName("Image_Door_Close")
        doorclose:setOpacity(0)
        open_er(unitIndex,classIndex)
    end

    --    local function xiaoKeTang(classIndex)
    --
    --    end

--    local function setStarAndApple(ScoreStar,ScoreApple) -- 设置场景右上方分数
--        local StarLabel =self._Panel_RightUi:getChildByName("Label_star_num")
--        StarLabel:setString(""..ScoreStar)
--        local AppleLabel = self._Panel_RightUi:getChildByName("Label_apple")
--        AppleLabel:setString(""..ScoreApple)
--    end

    local function getGameOrder(unitIndex,classIndex,gameIndex)
        local _unit_id = local_data.get_unit_id_by_unitIndex(gameManger.getCurSatrt(),unitIndex)
        local gameId = local_data.get_gameId_by_cIdx_gIdx(gameManger.getCurSatrt(),_unit_id,classIndex,gameIndex)
        local gameOrder = local_data.get_game_order_classType_gameId(gameManger.getCurSatrt(),gameId)
        return gameOrder
    end
    local function getUnitClassGameIndexByOrder(gameOrder)
        local courseId = local_data.get_courseId_by_gameOrder(gameManger.getCurSatrt(),gameOrder)
        local unitId = local_data.get_unitId_by_gameOrder(gameManger.getCurSatrt(),gameOrder)
        local gameId = local_data.get_gameId_by_gameOrder(gameManger.getCurSatrt(),gameOrder)
        local _unit_Index = local_data.get_unit_index_class(gameManger.getCurSatrt(),unitId)
        local _class_Index = local_data.get_course_index_class_and_unit(gameManger.getCurSatrt(),unitId,courseId)
        local _game_Index = local_data.get_game_index_course_byid(gameId)
        return _unit_Index,_class_Index,_game_Index
    end
    local function JumpFunc(unitIndex,classIndex, gameIndex) -- 页面跳转
        local _root = self._scrollView:getChildByName("Panel_danyuan_"..unitIndex)
        local gameOrder = getGameOrder(unitIndex,classIndex, gameIndex)
        if CurGameOrder~=nil and CurGameOrder < gameOrder then -- 所玩游戏小于游戏进度
            cclog("----->"..CurGameOrder)
            unitIndex,classIndex,gameIndex = getUnitClassGameIndexByOrder(CurGameOrder)
            cclog("---dd->"..unitIndex)
            _root = self._scrollView:getChildByName("Panel_danyuan_"..unitIndex)
            cclog("classvIndex:"..classIndex.."gameIndex:"..gameIndex)
        else
            cclog("90000s")
            if _gameList==nil then _gameList=local_data.get_game_list_by_classType(gameManger.getCurSatrt()) end--当前星球游戏列表**
            _gameScoreList=gameManger.get_score_list()
            if #_gameScoreList>=#_gameList then --已经玩完所有游戏
                local classTypeIndex = get_index()--星球索引
                if classTypeIndex==1 then
                    unitIndex = cc.UserDefault:getInstance():getIntegerForKey(client.Info.userInfo.user_id.."XSUnitIdx")
                    classIndex = cc.UserDefault:getInstance():getIntegerForKey(client.Info.userInfo.user_id.."XSClassIdx")
                    gameIndex = cc.UserDefault:getInstance():getIntegerForKey(client.Info.userInfo.user_id.."XSGameIdx")
                elseif classTypeIndex==2 then
                    unitIndex = cc.UserDefault:getInstance():getIntegerForKey(client.Info.userInfo.user_id.."SSUnitIdx")
                    classIndex = cc.UserDefault:getInstance():getIntegerForKey(client.Info.userInfo.user_id.."SSClassIdx")
                    gameIndex = cc.UserDefault:getInstance():getIntegerForKey(client.Info.userInfo.user_id.."SSGameIdx")
                else
                    unitIndex = cc.UserDefault:getInstance():getIntegerForKey(client.Info.userInfo.user_id.."BSUnitIdx")
                    classIndex = cc.UserDefault:getInstance():getIntegerForKey(client.Info.userInfo.user_id.."BSClassIdx")
                    gameIndex = cc.UserDefault:getInstance():getIntegerForKey(client.Info.userInfo.user_id.."BSGameIdx")
                end
                
                cclog("-----unitIndex--->"..unitIndex)
                cclog("-----classIndex--->"..classIndex)
                cclog("-----classIndex--->"..gameIndex)
                if classIndex== 0 then
                    unitIndex,classIndex,gameIndex = getUnitClassGameIndexByOrder(#_gameList)
                end
            end
        end
        cclog("--->"..unitIndex)
        cclog("--->"..classIndex)
        cclog("--->"..gameIndex)
        local _root = self._scrollView:getChildByName("Panel_danyuan_"..unitIndex)
        local _openIcon = _root:getChildByName("Panel_Class_"..classIndex):getChildByName("Panel_game_"..gameIndex):getChildByName("Image_icon_open")
        local _worldPoint = _openIcon:convertToWorldSpaceAR(cc.p(0,0))
        cclog("ttt"..self._scrollView:getInnerContainerSize().width)
        cclog("--->jjjjj".._cur_x)
        --------------------------------
        --    local _openIcon = _root:getChildByName("Panel_Class_"..classIndex):getChildByName("Panel_game_1"):getChildByName("Image_icon_open")
        --    local _worldPoint = _openIcon:convertToWorldSpaceAR(cc.p(0,0))
        ---------------------    
        local xxx= self._scrollView:convertToNodeSpace(_worldPoint)
        --cclog("xxx:"..xxx.x)
        --    --cclog("ttt"..self._scrollView:getInnerContainerSize().width)
        --    local vv = self._scrollView:getInnerContainer():getPositionX()
        local scrollW=self._scrollView:getContentSize().width
        local _cha = xxx.x-scrollW/2
        if _cha<0 then
            _cha = 0
        end
        local offset = self._scrollView:getInnerContainerSize().width-self._scrollView:getContentSize().width
        local _per=0
        if offset~=0 then
            _per=math.abs(_cha)*100/offset
        end
        --cclog("------lll>".._per)
        --
        self._scrollView:jumpToPercentHorizontal(_per)
        --------------------------
    end

    local function Open_Door_Special( unitIndex,classIndex ,Func) -- 开门特效
        _root = self._scrollView:getChildByName("Panel_danyuan_"..unitIndex)
        local doorclose = _root:getChildByName("Panel_Class_"..classIndex):getChildByName("Panel_Door"):getChildByName("Image_Door_Close")
        local dd = cc.ParticleSystemQuad:create("particle/game_main_map1/foguang.plist")
        dd:setPosition(doorclose:getPositionX()+10, doorclose:getPositionY()-20)
        dd:setPositionType(cc.POSITION_TYPE_RELATIVE)
        doorclose:getParent():addChild(dd,55,55)
        dd:runAction(cc.Sequence:create(cc.DelayTime:create(0.9),cc.FadeOut:create(0.3),cc.RemoveSelf:create()))
        ccs.ActionManagerEx:getInstance():playActionByName(get_ui(),"game_"..unitIndex.."_"..classIndex.."_door_Ac",cc.CallFunc:create(function ( )
            -- 小课堂的牌子：如果这一课门打开，小课堂牌子就出现，永远不消失
            Func()
            
            open_er(unitIndex,classIndex)
        end))

    end
    local gameList=local_data.get_game_list_by_classType(gameManger.getCurSatrt())
    table_out("gameList",gameList)
    local function Init_Game_Button(unitIndex,classIndex, gameIndex,score)
        _root = self._scrollView:getChildByName("Panel_danyuan_"..unitIndex)
        --cclog("classvpIndex:"..classIndex.."gameIndex:"..gameIndex)
        local _game = _root:getChildByName("Panel_Class_"..classIndex):getChildByName("Panel_game_"..gameIndex)
        --        _game:getChildByName("Image_icon_open"):setOpacity(255) -- 变红
        --设置进关按钮
        local _button = _game:getChildByName("Button")
        --        if _button:isTouchEnabled() == false then
        --        	_button:setTouchEnabled(true)
        --        end
        _button:setTouchEnabled(false)

        if isNewGame == true then
            self:runAction(cc.Sequence:create(cc.DelayTime:create(1.0),cc.CallFunc:create(function() -- 这种情况弹窗直接弹出
                _button:setTouchEnabled(true)
            end)))
        else
            _button:setTouchEnabled(true)
        end
        _button:addTouchEventListener(function(sender,eventType)
            if _game:getChildByName("Image_icon_open"):getOpacity() == 0 then return end
            if eventType == ccui.TouchEventType.ended and self._isMini==false then
                --                            if _data.open~=true then return end
                if self._canTouch == false then
                    --                            --cclog("已经触摸某一个按钮了")
                    return
                end--by mitsui
                --                AudioEngine.playEffect("uimusic/anniu.mp3",false)
                self:show_mini_layer(get_index(),unitIndex,classIndex,gameIndex,score)
            end
        end)

    end

    local function Init_Game_Point( unitIndex,classIndex, gameIndex, _score ) -- 普通初始化饼
        local _root = self._scrollView:getChildByName("Panel_danyuan_"..unitIndex)
        local starNum=gameManger.change_scoreToStart(_score)
        local _game = _root:getChildByName("Panel_Class_"..classIndex):getChildByName("Panel_game_"..gameIndex)
        _game:getChildByName("Image_icon_open"):setOpacity(255) -- 变红
        local _Panel_Num = _game:getChildByName("Panel_Num")
        for key, var in pairs(_Panel_Num:getChildren()) do -- 设置饼上的数字
            local s_tr = string.sub(var:getName(),7,7)
            var:loadTexture("numble/ditu_"..s_tr..".png")
        end
        if starNum > 0 then
            for i=1,starNum do
                _root:getChildByName("Panel_Class_"..classIndex):getChildByName("Panel_game_"..gameIndex):getChildByName("Image_Star_"..i):setOpacity(255)
            end
        end
    end

    local function setHead(unitIndex,classIndex, gameIndex )
        if _gameList==nil then   _gameList=local_data.get_game_list_by_classType(gameManger.getCurSatrt()) end --当前星球游戏列表**
        _gameScoreList=gameManger.get_score_list()
        if #_gameScoreList>=#_gameList then --已经玩完所有游戏
            local classTypeIndex = get_index()--星球索引
            if CurGameOrder~=nil then
                --            self._class_index = classIndex
                --            self._game_index = gameIndex
                ----------如果所有游戏已经玩完------------------------
                --记录当前进度，即下次人头所在位置
                
                if classTypeIndex==1 then
                    cc.UserDefault:getInstance():setIntegerForKey(client.Info.userInfo.user_id.."XSUnitIdx", unitIndex)
                    cc.UserDefault:getInstance():flush()
                    cc.UserDefault:getInstance():setIntegerForKey(client.Info.userInfo.user_id.."XSClassIdx", classIndex)
                    cc.UserDefault:getInstance():flush()
                    cc.UserDefault:getInstance():setIntegerForKey(client.Info.userInfo.user_id.."XSGameIdx", gameIndex)
                    cc.UserDefault:getInstance():flush()
                elseif classTypeIndex==2 then 
                    cc.UserDefault:getInstance():setIntegerForKey(client.Info.userInfo.user_id.."SSUnitIdx", unitIndex)
                    cc.UserDefault:getInstance():flush()
                    cc.UserDefault:getInstance():setIntegerForKey(client.Info.userInfo.user_id.."SSClassIdx", classIndex)
                    cc.UserDefault:getInstance():flush()
                    cc.UserDefault:getInstance():setIntegerForKey(client.Info.userInfo.user_id.."SSGameIdx", gameIndex)
                    cc.UserDefault:getInstance():flush()
                else
                    cc.UserDefault:getInstance():setIntegerForKey(client.Info.userInfo.user_id.."BSUnitIdx", unitIndex)
                    cc.UserDefault:getInstance():flush()
                    cc.UserDefault:getInstance():setIntegerForKey(client.Info.userInfo.user_id.."BSClassIdx", classIndex)
                    cc.UserDefault:getInstance():flush()
                    cc.UserDefault:getInstance():setIntegerForKey(client.Info.userInfo.user_id.."BSGameIdx", gameIndex)
                    cc.UserDefault:getInstance():flush()
                end
                --            --cclog("7880::"..cc.UserDefault:getInstance():getIntegerForKey("XSGameIdx"))
                ----------------------------------
            else
                if classTypeIndex==1 then
                    unitIndex = cc.UserDefault:getInstance():getIntegerForKey(client.Info.userInfo.user_id.."XSUnitIdx")
                    classIndex = cc.UserDefault:getInstance():getIntegerForKey(client.Info.userInfo.user_id.."XSClassIdx")
                    gameIndex = cc.UserDefault:getInstance():getIntegerForKey(client.Info.userInfo.user_id.."XSGameIdx")
                elseif classTypeIndex==2 then
                    unitIndex = cc.UserDefault:getInstance():getIntegerForKey(client.Info.userInfo.user_id.."SSUnitIdx")
                    classIndex = cc.UserDefault:getInstance():getIntegerForKey(client.Info.userInfo.user_id.."SSClassIdx")
                    gameIndex = cc.UserDefault:getInstance():getIntegerForKey(client.Info.userInfo.user_id.."SSGameIdx")
                else
                    unitIndex = cc.UserDefault:getInstance():getIntegerForKey(client.Info.userInfo.user_id.."BSUnitIdx")
                    classIndex = cc.UserDefault:getInstance():getIntegerForKey(client.Info.userInfo.user_id.."BSClassIdx")
                    gameIndex = cc.UserDefault:getInstance():getIntegerForKey(client.Info.userInfo.user_id.."BSGameIdx")
                end
                if classIndex==0 then -- 如果没有本地记录，头像为最后一个游戏的位置
                    --cclog("没有本地记录，头像为最后一个游戏的位置")
                    unitIndex,classIndex,gameIndex = getUnitClassGameIndexByOrder(#_gameList)
                end
            end
        end
        cclog("unitInx"..unitIndex.."classInx"..classIndex.."-->"..gameIndex)
--        local nowIndex = (classIndex-1)*3+gameIndex
        local _root = self._scrollView:getChildByName("Panel_danyuan_"..unitIndex)
        local _OpenIcon = _root:getChildByName("Panel_Class_"..classIndex):getChildByName("Panel_game_"..gameIndex):getChildByName("Image_icon_open")
        local Worldp = _OpenIcon:convertToWorldSpaceAR(cc.p(0,0))
        local posX = self._scrollView:getInnerContainer():getPositionX() -- scrollView offset
        local nowp = self._scrollView:convertToNodeSpace(Worldp)
        if gameIndex == 1 or isNewGame == false  then
            --            local _root = self.panel_Danyuan_1
            cclog("kkkkkkkff")
            self._Panel_pet:setPosition(cc.p(nowp.x-posX-43,nowp.y+42))
            self._Panel_pet:setVisible(true)
        else
            local nowGameOrder = getGameOrder(unitIndex,classIndex,gameIndex)
            local preGameOrder = nowGameOrder-1
            local unitIndexPre,classIndexPre,gameIndexPre = getUnitClassGameIndexByOrder(preGameOrder)
            cclog("classIndexPre:"..classIndexPre.."-gameIndexPre:"..gameIndexPre)
            local _root = self._scrollView:getChildByName("Panel_danyuan_"..unitIndexPre)
            local preOpenIcon = _root:getChildByName("Panel_Class_"..classIndexPre):getChildByName("Panel_game_"..gameIndexPre):getChildByName("Image_icon_open")
            local preWorldp = preOpenIcon:convertToWorldSpaceAR(cc.p(0,0))
            local prep = self._scrollView:convertToNodeSpace(preWorldp)
            self._Panel_pet:setPosition(cc.p(prep.x-posX-43,prep.y+42))
            self._Panel_pet:setVisible(true)
            self._Panel_pet:runAction(cc.JumpTo:create(1.0,cc.p(nowp.x-posX-43,nowp.y+42),50,1))
            local _now_x=self._scrollView:getInnerContainer():getPositionX()
            cclog("7788:".._now_x)
            local _X = nowp.x - prep.x
            local jindu = _now_x-_X
            if jindu >0 then
                jindu = 0
            end
            local offset = self._scrollView:getInnerContainerSize().width-self._scrollView:getContentSize().width
            local _percent = 0
            if offset~=0 then
                local vvv= (_now_x)*100/offset
                _percent=math.abs(jindu)*100/offset
            end
            cclog("-------_X:".._X)
            cclog("-------percent:".._percent)
            --            self._scrollView:jumpToPercentHorizontal(vvv)
            self._scrollView:scrollToPercentHorizontal(_percent,1.0,true)
        end
    end

    local function Init_Head_And_Point( unitIndex,classIndex, gameIndex ) --参数：最新的那个图标 -- 初始化头像和它下面饼的状态
        cclog("Panel_Unit_"..unitIndex.."Panel_Class_"..classIndex.."Panel_game_"..gameIndex)
--        local numstr = string.format("%02d",(classIndex-1)*3+gameIndex) -- 保存后面用
        if _gameList==nil then _gameList=local_data.get_game_list_by_classType(gameManger.getCurSatrt()) end--当前星球游戏列表**
        local _gameScoreList=gameManger.get_score_list()
        if #_gameScoreList<#_gameList then
            ccs.ActionManagerEx:getInstance():playActionByName(get_ui(),"game_"..unitIndex.."_"..classIndex.."_"..gameIndex.."_icon_Ac")
        end
        if CurGameOrder~=nil then
            local gameOrder = getGameOrder(unitIndex,classIndex,gameIndex)
            if CurGameOrder < gameOrder then -- 玩的是以前的游戏
                --cclog("777777777")
                unitIndex,classIndex,gameIndex=getUnitClassGameIndexByOrder(CurGameOrder)
                cclog("unitIndex-:"..unitIndex.."classIndex-:"..classIndex.."gameIndex:"..gameIndex)
            end
        end
        setHead(unitIndex,classIndex,gameIndex)
    end

    local function Set_Box_State(unitIndex,_course_index,_state)--——_state(0未完成1已完成2已开启)
        _root = self._scrollView:getChildByName("Panel_danyuan_"..unitIndex)
        local _unit_id = local_data.get_unit_id_by_unitIndex(gameManger.getCurSatrt(),unitIndex)
--        if i==1 then return end
--        local _courseList = local_data.get_course_List_class_and_unit(gameManger.getCurSatrt(),_unit_id,)
        local _course_id = local_data.get_courseID_by_claId_uniId_Index(gameManger.getCurSatrt(),_unit_id,_course_index)--_courseList[_course_index]
--        local _course_index=local_data.get_course_index_class_and_courseId(gameManger.getCurSatrt(),_course_id)
        local boxPanel = _root:getChildByName("Panel_Class_".._course_index):getChildByName("Panel_Box")
        local boxButton = boxPanel:getChildByName("Button")
        --        boxButton:setOpacity(255)
        if _state==0 then
            --cclog("9星未完成")
            boxButton:addTouchEventListener(function(sender,eventType)
                if eventType == ccui.TouchEventType.ended  then
                    require "waitLoad"
                    local musicId = AudioEngine.playEffect("lock02.mp3",false)
                    waitLoad.show_err_mes("本课都获得满星才能打开宝箱哦！",function ()
                        if musicId~=nil then
                            AudioEngine.stopEffect(musicId)
                            musicId=nil
                        end
                        waitLoad.dell_err_mes()
                    end,function ()
                        if musicId~=nil then
                            AudioEngine.stopEffect(musicId)
                            musicId=nil
                        end
                        waitLoad.dell_err_mes()
                    end)

                end
            end)
            return
        end
        boxButton:addTouchEventListener(function(sender,eventType)
            if eventType == ccui.TouchEventType.ended  then
                require "boxes_choose"
                --cclog("_course_id--->".._course_id)
                local musicId = AudioEngine.playEffect("lock01.mp3",false)
                local function update(dt)
                    --cclog("update: " ) -- 输出log
                    require "boxes_choose"
                    if musicId~=nil and boxes_choose:isLayerNil() then
                        --                    --cclog("gusn")
                        AudioEngine.stopEffect(musicId)
                        musicId=nil
                    end
                    self.boxTag = boxes_choose:isopen()
                    if self.boxTag==false then
                        --cclog("未打开")
                    end
                    if self.boxTag == true then
                        --cclog("箱子已打开")
                        AudioEngine.playEffect("uimusic/baoxiang.mp3",false)
                        boxPanel:unscheduleUpdate()
                        if musicId~=nil  then
                            --                                    --cclog("gusn")
                            AudioEngine.stopEffect(musicId)
                            musicId=nil
                        end
                        local baoxiang = boxPanel:getChildByName("Image_Box_Open")
                        baoxiang:setOpacity(0)
                        baoxiang:getParent():removeChildByTag(333)
                        local boxButton = boxPanel:getChildByName("Button")
                        boxButton:setTouchEnabled(false)
                        ---------update2--------------
                        --                        local tag = true -- 控制变量
                        local function update2(dt)
                            if boxes_choose:isLayerNil()==true then

                                
                                baoxiang:unscheduleUpdate()
                                local p =cc.ParticleSystemQuad:create("particle/box/box_star_getScore.plist")
                                local Bworldp = baoxiang:convertToWorldSpaceAR(cc.p(0,0))
                                local pos = self._rootPanel:convertToNodeSpace(Bworldp)
                                p:setPosition(pos)
                                self._rootPanel:addChild(p,333)
                                p:runAction(cc.Sequence:create(cc.MoveTo:create(1.5,cc.p(self._Panel_RightUi:getPositionX()+90,self._Panel_RightUi:getPositionY()+55)),cc.CallFunc:create(function()
                                    AudioEngine.playEffect("uimusic/addstar.mp3",false)

                                    local StarLabel =self._Panel_RightUi:getChildByName("Label_star_num")
                                    local num = tonumber(StarLabel:getString())
                                    num = num + boxes_choose.getBoxScore()
                                    StarLabel:setString(""..num)
                                    p:removeFromParent(true)
                                end)))

                            end
                        end
                        baoxiang:scheduleUpdateWithPriorityLua(update2, 0)
                        ------------------------------
                    elseif self.boxTag == false and boxes_choose:isLayerNil()==true then
                        boxPanel:unscheduleUpdate()
                    end
                end

                boxPanel:scheduleUpdateWithPriorityLua(update, 0)
                --                boxes_choose.show_BoxPage(_course_id)
                boxes_choose.show_BoxPage(_course_id,function()
                    --                    netWork.get_total_star(function (_state,_mes)--动态更新xx
                    --                        if _state==200 then
                    --                            AppleLabel:setString(""..client.Info.userInfo.apple_count)
                    --                            StarLabel:setString(""..client.Info.userInfo.star_count)
                    --                    end
                    --                    end)
                    end)
            end
        end)
        if _state == 1 then
            local baoxiang = boxPanel:getChildByName("Image_Box_Open")
            baoxiang:loadTexture("map_1_box.png")
            local baoxianglizi = cc.ParticleSystemQuad:create("particle/game_main_map1/gameMapDoor.plist")
            baoxianglizi:setPosition(baoxiang:getPositionX()+5, baoxiang:getPositionY())
            baoxianglizi:setPositionType(cc.POSITION_TYPE_RELATIVE)
            baoxiang:getParent():addChild(baoxianglizi,20,333)
        elseif _state == 2 then
            local baoxiang = boxPanel:getChildByName("Image_Box_Open")
            baoxiang:setOpacity(0)
            local boxButton = boxPanel:getChildByName("Button")
            boxButton:setTouchEnabled(false)
        end
        self.boxTag = false

    end
    --    Init_Head_And_Point(3,1)
    --cclog("---------->>>>>")
    -------------- 相关方法结束 ------------------------
    
    local _unitList=gameManger.get_unit_list()--单元列表
    local _courseList=gameManger.get_course_list()--课程列表
    local _gameScoreList=gameManger.get_score_list()--游戏列表
    ---------
    table_out("_gameScoreList",_gameScoreList)
    local classTypeIndex = get_index()--星球索引
    if classTypeIndex==1 then
        CurGameOrder = XSMainMapSceneCurGameOrder
    elseif classTypeIndex==2 then
        CurGameOrder = SSMainMapSceneCurGameOrder
    else
        CurGameOrder = BSMainMapSceneCurGameOrder
    end
    if _gameList==nil then  _gameList=local_data.get_game_list_by_classType(gameManger.getCurSatrt())  end--当前星球游戏列表**
    local function _get_course_total_star(_course_id)
        local _num=0
        for k,v in ipairs(_gameScoreList) do
            if v.course_id==_course_id then
                _num=_num+v.score
            end
        end
        return (_num==300)
    end
    for k,v in ipairs(_gameList) do
        local _unit_index=local_data.get_unit_index_class(gameManger.getCurSatrt(),v.unit_id)
        local _course_index=local_data.get_course_index_class_and_unit(gameManger.getCurSatrt(),v.unit_id,v.course_id)
        local _game_index=local_data.get_game_index_course(v.course_id,v.id)
        local _score=0
        if _gameScoreList~=nil and _gameScoreList[k]~=nil then
            _score=_gameScoreList[k].score
        end
        --cclog("--->_course_index".._course_index)
        --cclog("--->_game_index".._game_index)
        Init_Game_Button(_unit_index,_course_index,_game_index,_score)
    end
    
    if gameManger.isFirst_comeIn()==true then  --第一次进入
        gameManger.set_isFirst_comeIn(false)
        Open_Door_Special(1,1,function()
            Init_Head_And_Point(1,1,1)
            Init_Game_Point( 1, 1, 1, 0 ) -- 数字颜色
        end)
        Set_Box_State(1,1,0)
        return
      end
    table_out("_courseList",_courseList)
    for k,v in ipairs(_courseList) do--初始化门
        local unitId = local_data.get_unit_id_by_courseId(v.course_id)
        local _unit_index=local_data.get_unit_index_class(gameManger.getCurSatrt(),unitId)
--        local _course_index=local_data.get_course_index_class_and_courseId_and_unit(gameManger.getCurSatrt(),unitId,v.course_id)
        local _course_index=local_data.get_course_index_class_and_unit(gameManger.getCurSatrt(),unitId,v.course_id)
        Open_Door(_unit_index,_course_index)
        ----------------------------------------宝箱相关
        if _get_course_total_star(v.course_id)==true then
            --cclog("v.course_id---->"..v.course_id)
            if v.award_state==0 then--完成未领取
                Set_Box_State(_unit_index,_course_index,1)
            else
                Set_Box_State(_unit_index,_course_index,2)
            end
        else
            Set_Box_State(_unit_index,_course_index,0)
        end
        -----------------------------------------
    end
    
    if _gameScoreList~=nil then
        for k,v in ipairs(_gameScoreList) do--初始化游戏成绩
            --cclog("gameId-->"..v.game_id)
            local _id=v.game_id
            local _score=v.score
            local _unit_index=local_data.get_unit_index_class(gameManger.getCurSatrt(),v.unit_id)
            local _course_index=local_data.get_course_index_class_and_unit(gameManger.getCurSatrt(),v.unit_id,v.course_id)
            local _game_index=local_data.get_game_index_course(v.course_id,v.game_id)
            Init_Game_Point(_unit_index,_course_index,_game_index,_score)
        end
        
        local _cur_game_index=#_gameScoreList+1--初始化下一游戏
        --    local _gameList=local_data.get_game_list_by_classType(gameManger.getCurSatrt())
        if _cur_game_index>#_gameList then
            cclog("最后一个游戏")
            isNewGame = false
            --            local v=_gameList[_cur_game_index]
            --            local _course_index=local_data.get_course_index_class_and_courseId(gameManger.getCurSatrt(),v.course_id)
            --            local _game_index=local_data.get_game_index_course(v.course_id,v.id)

            --unitIndex,classIndex,gameIndex= getGameOrder(unitIndex,classIndex,gameIndex)
            --locsl p = getUnitClassGameIndexByOrder(gameOrder)
            local lastUnitIdex,lastClassIdex,lastGameIdex = getUnitClassGameIndexByOrder(#_gameList)
            JumpFunc(lastUnitIdex,lastClassIdex,lastGameIdex)
            Init_Head_And_Point(lastUnitIdex,lastClassIdex,lastGameIdex)--此地图最后一个游戏的位置

        else
            cclog("不是最后一个游戏")
            local v=_gameList[_cur_game_index]
            local _unit_index=local_data.get_unit_index_class(gameManger.getCurSatrt(),v.unit_id)
            local _course_index=local_data.get_course_index_class_and_unit(gameManger.getCurSatrt(),v.unit_id,v.course_id)
            local _game_index=local_data.get_game_index_course(v.course_id,v.id)
            if gameManger.is_open_course(v.course_id)==true then
                Init_Game_Point(_unit_index,_course_index,_game_index,0) -- 字体颜色
                JumpFunc(_unit_index,_course_index, _game_index)
                Init_Head_And_Point(_unit_index,_course_index,_game_index)
                ------debug----------
                --弹出下一游戏弹窗
                if isNewGame==true and isCloseEnd == false then
                    if self._canTouch == false then
                        --                            --cclog("已经触摸某一个按钮了")
                        return end--by mitsui
                    self:show_mini_layer(get_index(),_unit_index,_course_index,_game_index,0)
                end
                -----debug----------
            end
        end
    end
--    if 1==1 then return end
    ----------------------------------判断是否开启下一课程---------------------------------
    if _courseList~=nil and #_courseList>0 then
        if _courseList[#_courseList].complete_time~=nil
            and _courseList[#_courseList].complete_time~="" then--开启下一课程
            
            local _course_id=_courseList[#_courseList].course_id
            local class_type=_courseList[#_courseList].class_type
            local unit_id = local_data.get_unit_id_by_courseId(_course_id)
            local _next_courseId=local_data.get_next_course_by_cId_uId(class_type,unit_id,_course_id) -- 是否有下一课的信息
--        --cclog("_next_courseId------->".._next_courseId)
            if _next_courseId~=nil then --打开课程
                gameManger.open_course(_next_courseId)
                local unitId=local_data.get_unit_id_by_courseId(_next_courseId)
                local nextUnitIndex=local_data.get_unit_index_class(gameManger.getCurSatrt(),unitId)
                local nextClassIndex=local_data.get_course_index_class_and_unit(gameManger.getCurSatrt(),unitId,_next_courseId)
                JumpFunc(nextUnitIndex,nextClassIndex,1)
                Open_Door_Special(nextUnitIndex,nextClassIndex,function ()
                    Init_Head_And_Point(nextUnitIndex,nextClassIndex,1)
                end)
                Set_Box_State(nextUnitIndex,nextClassIndex,_next_courseId,0)
            else --v1.0.0.1开启新的单元
                local nextUnitId=local_data.get_next_unit(class_type,unit_id)
                if nextUnitId==nil then return end
                --cclog("---nextUnitId----->"..nextUnitId)
                local nextCourseId =local_data.get_courseID_by_claId_uniId_Index(gameManger.getCurSatrt(),nextUnitId,1)
                --cclog("---nextCourseId----->"..nextCourseId)
                gameManger.open_course(nextCourseId)
                local nextUnitIndex=local_data.get_unit_index_class(gameManger.getCurSatrt(),nextUnitId)
                JumpFunc(nextUnitIndex,1,1)
                Open_Door_Special(nextUnitIndex,1,function ()
                    Init_Head_And_Point(nextUnitIndex,1,1)
                end)
                Set_Box_State(nextUnitIndex,1,0)    
            end
        end
    end

end

function XSMainMapScene:initScrollViewTouch()
    self._scrollView:addTouchEventListener(function(sender,eventType)
        if eventType==ccui.TouchEventType.began then
            --cclog("scrollviewTouch began")
            local pos = sender:getTouchBeganPosition()
            local _now_x=self._scrollView:getInnerContainer():getPositionX()
            --            local pa = cc.ParticleSystemQuad:create("particle/game_main_map1/touchDown.plist")
            local pa = cc.ParticleSystemQuad:create(get_touch())
            pa:setPosition(pos.x-_now_x,pos.y)
            pa:setPositionType(cc.POSITION_TYPE_RELATIVE)
            self._scrollView:addChild(pa,20,333)
            pa:runAction(cc.Sequence:create(cc.DelayTime:create(1.5),cc.RemoveSelf:create()))
        elseif eventType==ccui.TouchEventType.moved then
            local pos = sender:getTouchMovePosition()
            local pa = cc.ParticleSystemQuad:create(get_move())
            local _now_x=self._scrollView:getInnerContainer():getPositionX()
            pa:setPosition(pos.x-_now_x,pos.y)
            pa:setPositionType(cc.POSITION_TYPE_RELATIVE)
            self._scrollView:addChild(pa,20,333)
            pa:runAction(cc.Sequence:create(cc.DelayTime:create(0.5),cc.RemoveSelf:create()))
        end
    end)
end

function XSMainMapScene:onEnter()
    --    gameManger.goStart("BX000000001",function()
    --        --cclog("XSMainMapScene---->onEnter")
    --
    --        end)
    netTag = true
    self:init()
    AudioEngine.stopMusic()
    AudioEngine.preloadMusic("play1.mp3")
    AudioEngine.setEffectsVolume(0.5)
    AudioEngine.setMusicVolume(0.5)
    AudioEngine.playMusic("play1.mp3",true)
end
function XSMainMapScene:onExit()
    --cclog("XSMainMapScene---onExit-->")
    netTag = false
    _gameList=nil
    _gameScoreList=nil
    AudioEngine.stopMusic()
    ccs.ArmatureDataManager:destroyInstance()
    ccs.ActionManagerEx:destroyInstance()
    ccs.GUIReader:destroyInstance()
    ccs.SceneReader:destroyInstance()
end

function XSMainMapScene.create()
    local scene = cc.Scene:create()
    local layer = XSMainMapScene.extend(cc.Layer:create())
    if nil ~= layer then
        local function onNodeEvent(event)
            if "enter" == event then
                layer:onEnter()
            elseif "exit" == event then
                layer:onExit()
            end
        end
        layer:registerScriptHandler(onNodeEvent)
    end
    scene:addChild(layer)
    return scene
end
function crt_XSMainMapScene()
    local _scene = XSMainMapScene.create()
    return _scene
end